package com.wages.project.system.wages.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageInfo;
import com.wages.common.utils.StringUtils;
import com.wages.project.system.user.domain.User;
import com.wages.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wages.framework.aspectj.lang.annotation.Log;
import com.wages.framework.aspectj.lang.enums.BusinessType;
import com.wages.project.system.wages.domain.Wages;
import com.wages.project.system.wages.service.IWagesService;
import com.wages.framework.web.controller.BaseController;
import com.wages.framework.web.domain.AjaxResult;
import com.wages.common.utils.poi.ExcelUtil;
import com.wages.framework.web.page.TableDataInfo;

/**
 * 工资信息Controller
 *
 * @author wages
 * @date 2021-02-08
 */
@Controller
@RequestMapping("/system/wages")
public class WagesController extends BaseController {
    private String prefix = "system/wages";

    @Autowired
    private IWagesService wagesService;
    @Autowired
    private IUserService userService;

    @RequiresPermissions("system:wages:view")
    @GetMapping()
    public String wages() {
        return prefix + "/wages";
    }

    /**
     * 查询工资信息列表
     */
    @RequiresPermissions("system:wages:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Wages wages) {
        startPage();

        List<Wages> list = wagesService.selectWagesList(wages);
        return getDataTable(list);
    }

    @PostMapping("/list1")
    @ResponseBody
    public Map list1(Wages wages) {
        startPage();
        wages.setUid(getUserId());
        if (null == wages.getTime()) {
            wages.setTime(new Date());
        }
        wages.setIsok("Y");
        List<Wages> list = wagesService.selectWagesList1(wages);
        User user = userService.selectUserById(getUserId());
        BigDecimal b = user.getWages();
        if (StringUtils.isNotEmpty(list)) {
            for (Wages wages1 : list) {
                if (wages1.getStatus().equals("Y")) {
                    b = b.add(wages1.getMonkey());
                } else {
                    b = b.subtract(wages1.getMonkey());
                }
            }

        }

        Map map = new HashMap();
        map.put("rows", list);
        map.put("wages", b);
        map.put("old", user.getWages());

        return map;
    }


    /**
     * 导出工资信息列表
     */
    @RequiresPermissions("system:wages:export")
    @Log(title = "工资信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Wages wages) {
        List<Wages> list = wagesService.selectWagesList(wages);
        ExcelUtil<Wages> util = new ExcelUtil<Wages>(Wages.class);
        return util.exportExcel(list, "wages");
    }

    /**
     * 新增工资信息
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工资信息
     */
    @RequiresPermissions("system:wages:add")
    @Log(title = "工资信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Wages wages) {
        return toAjax(wagesService.insertWages(wages));
    }

    @Log(title = "工资信息", businessType = BusinessType.INSERT)
    @PostMapping("/add1")
    @ResponseBody
    public AjaxResult add(Wages wages) {
        wages.setUid(getUserId());
        wages.setStatus("N");
        return toAjax(wagesService.insertWages(wages));
    }

    /**
     * 修改工资信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Wages wages = wagesService.selectWagesById(id);
        mmap.put("wages", wages);
        return prefix + "/edit";
    }

    /**
     * 修改保存工资信息
     */
    @RequiresPermissions("system:wages:edit")
    @Log(title = "工资信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Wages wages) {
        return toAjax(wagesService.updateWages(wages));
    }

    /**
     * 删除工资信息
     */
    @RequiresPermissions("system:wages:remove")
    @Log(title = "工资信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wagesService.deleteWagesByIds(ids));
    }
}
